package com.wosai.medical.hospital.demo.controller.register;

import com.google.common.base.Strings;
import com.google.common.collect.Lists;
import com.wosai.medical.hospital.demo.common.DataResult;
import com.wosai.medical.hospital.demo.controller.BaseController;
import com.wosai.medical.hospital.demo.controller.register.request.DoctorsReq;
import com.wosai.medical.hospital.demo.controller.register.request.PreRegisterReq;
import com.wosai.medical.hospital.demo.controller.register.request.RegisterReq;
import com.wosai.medical.hospital.demo.controller.register.request.SchedulesReq;
import com.wosai.medical.hospital.demo.controller.register.response.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.io.Serializable;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @desc
 * @auther 韦湘平
 * @date 2020/2/20 11:12 上午
 */

@Slf4j
@Api(tags = "挂号")
@RestController
@RequestMapping("/hospital/register")
public class RegisterController extends BaseController {

    /**
     * 2.1.可挂号科室信息
     *
     * @param apiAppId   接入账号
     * @param apiVersion 接口版本号
     * @param apiStamp   时间戳
     * @param apiSign    参数签名
     * @return 可挂号科室信息
     */
    @GetMapping("/depts")
    @ApiOperation("2.1. 可挂号科室信息")
    public RespResult<DataResult> getDepts(
            //接入账号(app_id)
            @RequestHeader("api_app_id") final String apiAppId,
            //接口版本号(采用正整数描述)
            @RequestHeader("api_version") final Integer apiVersion,
            //时间戳(13位到毫秒)
            @RequestHeader("api_stamp") final Long apiStamp,
            //参数签名
            @RequestHeader("api_sign") final String apiSign

    ) {
        return action(new Process<DataResult>() {
            @Override
            public String getAppId() {
                return apiAppId;
            }

            @Override
            public Integer getVersion() {
                return apiVersion;
            }

            @Override
            public Long getStamp() {
                return apiStamp;
            }

            @Override
            public String getSign() {
                return apiSign;
            }


            @Override
            public DataResult handler() {
                ///TODO: 业务处理
                List<Serializable> depts = Lists.newArrayList(
                        Depts.builder()
//                                .category(Depts.Category.builder().code("0001").name("测试科室分类").build())
                                .code("0001")
                                .name("测试科室1")
                                .tel("120")
                                .intro("测试科室1")
                                .location("科室位置[可空]")
                                .build(),
                        Depts.builder()
//                                .category(Depts.Category.builder().code("0002").name("测试科室分类").build())
                                .code("0002")
                                .name("测试科室2")
                                .tel("121")
                                .intro("测试科室2")
                                .location("科室位置[可空]")
                                .build()
                );
                return DataResult.builder().totals(1L).rows(depts).build();
            }
        });
    }

    /**
     * 2.2可挂号医生信息
     *
     * @param apiAppId   接入账号
     * @param apiVersion 接口版本号
     * @param apiStamp   时间戳
     * @param apiSign    参数签名
     * @return 可挂号医生信息
     */
    @PostMapping("/doctors")
    @ApiOperation("2.2 可挂号医生信息")
    public RespResult<DataResult> getDoctors(
            //接入账号(app_id)
            @RequestHeader("api_app_id") final String apiAppId,
            //接口版本号(采用正整数描述)
            @RequestHeader("api_version") final Integer apiVersion,
            //时间戳(13位到毫秒)
            @RequestHeader("api_stamp") final Long apiStamp,
            //参数签名
            @RequestHeader("api_sign") final String apiSign,
            @RequestBody(required = false) final DoctorsReq req

    ) {
        return action(new Process<DataResult>() {
            @Override
            public String getAppId() {
                return apiAppId;
            }

            @Override
            public Integer getVersion() {
                return apiVersion;
            }

            @Override
            public Long getStamp() {
                return apiStamp;
            }

            @Override
            public String getSign() {
                return apiSign;
            }

            @Override
            public Map<String, Serializable> getParams() {
                return new HashMap<String, Serializable>(2) {
                    {
                        //科室代码
                        put("deptCode", req.getDeptCode());
                        //医生代码
                        put("doctorCode", req.getDoctorCode());
                    }
                };
            }

            @Override
            public DataResult handler() {
                ///TODO: 业务处理
                List<Serializable> doctors = Lists.newArrayList(
                        Doctors.builder()
                                .dept(Doctors.Dept.builder()
                                        .code(req.getDeptCode())
                                        .name(req.getDeptCode().equals("0001")?"测试科室1":"测试科室2").build())
                                .code("110")
                                .name("测试医生")
                                .title("副主任医师")
                                .gender(1)
                                .tel("120")
                                .skill("yyyy")
                                .intro("dddddd")
                                .pic("http")
                                .schedules(Lists.newArrayList(
                                ))
                                .build()
                );
                return DataResult.builder().totals(1L).rows(doctors).build();


            }
        });
    }

    /**
     * 2.3号源信息
     *
     * @param apiAppId   接入账号
     * @param apiVersion 接口版本号
     * @param apiStamp   时间戳
     * @param apiSign    参数签名
     * @return 可挂号医生信息
     */
    @PostMapping("/schedules")
    @ApiOperation("2.3 号源信息")
    public RespResult<DataResult> getschedules(
            //接入账号(app_id)
            @RequestHeader("api_app_id") final String apiAppId,
            //接口版本号(采用正整数描述)
            @RequestHeader("api_version") final Integer apiVersion,
            //时间戳(13位到毫秒)
            @RequestHeader("api_stamp") final Long apiStamp,
            //参数签名
            @RequestHeader("api_sign") final String apiSign,
            @RequestBody(required = false) final SchedulesReq req

    ) {
        return action(new Process<DataResult>() {
            @Override
            public String getAppId() {
                return apiAppId;
            }

            @Override
            public Integer getVersion() {
                return apiVersion;
            }

            @Override
            public Long getStamp() {
                return apiStamp;
            }

            @Override
            public String getSign() {
                return apiSign;
            }

            @Override
            public Map<String, Serializable> getParams() {
                return new HashMap<String, Serializable>(2) {
                    {
                        //科室代码
                        put("deptCode", req.getDeptCode());
                        put("type", req.getType()); //挂号类型(1:当天挂号,2:预约挂号)
                        put("doctorCategory", req.getDoctorCategory());
                        //医生代码
                        put("doctorCode", req.getDoctorCode());
                        put("beginDate", req.getBeginDate());
                        put("endDate", req.getEndDate());
                    }
                };
            }

            @Override
            public DataResult handler() {
                String strDate=null;
                SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
               if (req.getType().equals(Integer.valueOf(1))) {
                   strDate=df.format( new Date());
               } else {
                   strDate=req.getBeginDate();
               }
               if (req.getType().equals(1)&&req.getDeptCode().equals("0001")){
                   ///TODO: 业务处理
                   List<Serializable> schedules = Lists.newArrayList(
                           Schedules.builder()
                                   .scheduleDate("2020-09-03")
                                   .depts(Lists.newArrayList(
                                           Schedules.Depts.builder()
                                                   .code("0001")
                                                   .name("测试科室1")
                                                   .doctors(Lists.newArrayList(
                                                           Schedules.Doctors.builder()
                                                                   .category(1)
                                                                   .code("110")
                                                                   .name("李伟")
                                                                   .schedules(Lists.newArrayList(
                                                                           Schedules.Schedule.builder()
                                                                                   .id("2020-09-03|249")
                                                                                   .timeFlag("17:40-17:45")
                                                                                   .timeName("下午")
                                                                                   .beginTime("17:40")
                                                                                   .endTime("17:45")
                                                                                   .totals(1)
                                                                                   .surpluses(1)
                                                                                   .regFee(1500L)
                                                                                   .treatFee(1000L)
                                                                                   .totalFee(2500L)
                                                                                   .build(),
                                                                           Schedules.Schedule.builder()
                                                                                   .id("2020-09-03|249")
                                                                                   .timeFlag("17:45-17:50")
                                                                                   .timeName("下午")
                                                                                   .beginTime("17:45")
                                                                                   .endTime("17:50")
                                                                                   .totals(1)
                                                                                   .surpluses(1)
                                                                                   .regFee(1500L)
                                                                                   .treatFee(1000L)
                                                                                   .totalFee(2500L)
                                                                                   .build(),
                                                                           Schedules.Schedule.builder()
                                                                                   .id("2020-09-03|249")
                                                                                   .timeFlag("17:50-17:55")
                                                                                   .timeName("下午")
                                                                                   .beginTime("17:50")
                                                                                   .endTime("17:55")
                                                                                   .totals(1)
                                                                                   .surpluses(1)
                                                                                   .regFee(1500L)
                                                                                   .treatFee(1000L)
                                                                                   .totalFee(2500L)
                                                                                   .build()
                                                                   ))
                                                                   .build()
                                                   ))
                                                   .build()
                                   ))

                                   .build()
                   );
                   return DataResult.builder().totals(1L).rows(schedules).build();
               } else  if (req.getType().equals(2)&&req.getDeptCode().equals("0002"))  {
                   ///TODO: 业务处理
                   List<Serializable> schedules = Lists.newArrayList(
                           Schedules.builder()
                                   .scheduleDate("2020-09-04")
                                   .depts(Lists.newArrayList(
                                           Schedules.Depts.builder()
                                                   .code("0002")
                                                   .name("测试科室2")
                                                   .doctors(Lists.newArrayList(
                                                           Schedules.Doctors.builder()
                                                                   .category(1)
                                                                   .code("110")
                                                                   .name("李伟")
                                                                   .schedules(Lists.newArrayList(
                                                                           Schedules.Schedule.builder()
                                                                                   .id("2020-09-04|250")
                                                                                   .timeFlag("17:40-17:45")
                                                                                   .timeName("下午")
                                                                                   .beginTime("17:40")
                                                                                   .endTime("17:45")
                                                                                   .totals(1)
                                                                                   .surpluses(1)
                                                                                   .regFee(1500L)
                                                                                   .treatFee(1000L)
                                                                                   .totalFee(2500L)
                                                                                   .build(),
                                                                           Schedules.Schedule.builder()
                                                                                   .id("2020-09-04|250")
                                                                                   .timeFlag("17:45-17:50")
                                                                                   .timeName("下午")
                                                                                   .beginTime("17:45")
                                                                                   .endTime("17:50")
                                                                                   .totals(1)
                                                                                   .surpluses(1)
                                                                                   .regFee(1500L)
                                                                                   .treatFee(1000L)
                                                                                   .totalFee(2500L)
                                                                                   .build(),
                                                                           Schedules.Schedule.builder()
                                                                                   .id("2020-09-04|250")
                                                                                   .timeFlag("17:50-17:55")
                                                                                   .timeName("下午")
                                                                                   .beginTime("17:50")
                                                                                   .endTime("17:55")
                                                                                   .totals(1)
                                                                                   .surpluses(1)
                                                                                   .regFee(1500L)
                                                                                   .treatFee(1000L)
                                                                                   .totalFee(2500L)
                                                                                   .build()
                                                                   ))
                                                                   .build()
                                                   ))
                                                   .build()
                                   ))

                                   .build()
                   );
                   return DataResult.builder().totals(1L).rows(schedules).build();
               } else {
                   List<Serializable> schedules = Lists.newArrayList();
                   return  DataResult.builder().totals(1L).rows(schedules).build();
               }
            }
        });
    }


    /**
     * 2.4 发起挂号
     *
     * @param apiAppId   接入账号
     * @param apiVersion 接口版本号
     * @param apiStamp   时间戳
     * @param apiSign    参数签名
     * @return 可挂号医生信息
     */
    @PostMapping("/patient/pre/created")
    @ApiOperation("2.4 发起挂号")
    public RespResult<PreRegister> preRegister(
            //接入账号(app_id)
            @RequestHeader("api_app_id") final String apiAppId,
            //接口版本号(采用正整数描述)
            @RequestHeader("api_version") final Integer apiVersion,
            //时间戳(13位到毫秒)
            @RequestHeader("api_stamp") final Long apiStamp,
            //参数签名
            @RequestHeader("api_sign") final String apiSign,
            @RequestBody(required = false) final PreRegisterReq req

    ) {
        return action(new Process<PreRegister>() {
                          @Override
                          public String getAppId() {
                              return apiAppId;
                          }

                          @Override
                          public Integer getVersion() {
                              return apiVersion;
                          }

                          @Override
                          public Long getStamp() {
                              return apiStamp;
                          }

                          @Override
                          public String getSign() {
                              return apiSign;
                          }

                          @Override
                          public Map<String, Serializable> getParams() {
                              return new HashMap<String, Serializable>(2) {
                                  {
                                      //科室代码
                                      put("patientCode", req.getPatientCode());
                                      put("visitDate", req.getVisitDate());
                                      put("deptCode", req.getDeptCode());
                                      //医生代码
                                      put("doctorCode", req.getDoctorCode());
                                      put("type", req.getType());
                                      put("spOrderCode", req.getSpOrderCode());
                                      put("spCreateTime", req.getSpCreateTime());
                                      put("schedule.id", req.getSchedule().getId());
                                      put("schedule.timeFlag", req.getSchedule().getTimeFlag());
                                      put("schedule.beginTime", req.getSchedule().getBeginTime());
                                      put("schedule.endTime", req.getSchedule().getEndTime());

                                  }
                              };
                          }

                          @Override
                          public PreRegister handler() {
                              ///TODO: 业务处理
                              return PreRegister.builder()
                                      .hisOrderCode(UUID.randomUUID().toString())
                                      .serialNum(5)
                                      .location("test")
                                      .regFee(0l)
                                      .treatFee(0l)
                                      .totalFee(0l)
                                      .description("hello")
                                      .build();


                          }
                      }
        );
    }

    /**
     * 2.5 挂号支付
     *
     * @param apiAppId   接入账号
     * @param apiVersion 接口版本号
     * @param apiStamp   时间戳
     * @param apiSign    参数签名
     * @return 可挂号医生信息
     */
    @PostMapping("/created")
    @ApiOperation("2.5 挂号支付")
    public RespResult<Register> register(
            //接入账号(app_id)
            @RequestHeader("api_app_id") final String apiAppId,
            //接口版本号(采用正整数描述)
            @RequestHeader("api_version") final Integer apiVersion,
            //时间戳(13位到毫秒)
            @RequestHeader("api_stamp") final Long apiStamp,
            //参数签名
            @RequestHeader("api_sign") final String apiSign,
            @RequestBody(required = true) final RegisterReq req

    ) {
        return action(new Process<Register>() {
                          @Override
                          public String getAppId() {
                              return apiAppId;
                          }

                          @Override
                          public Integer getVersion() {
                              return apiVersion;
                          }

                          @Override
                          public Long getStamp() {
                              return apiStamp;
                          }

                          @Override
                          public String getSign() {
                              return apiSign;
                          }

                          @Override
                          public Map<String, Serializable> getParams() {
                              return new HashMap<String, Serializable>(2) {
                                  {
                                      put("hisOrderCode", req.getHisOrderCode());
                                      put("spOrderCode", req.getSpOrderCode());
                                      put("payAmount", req.getPayAmount());
                                      put("payTime", req.getPayTime());
                                      put("payType", req.getPayType());
                                      put("payOrderCode", req.getPayOrderCode());
                                  }
                              };
                          }

                          @Override
                          public Register handler() {
                              ///TODO: 业务处理
                              return Register.builder()
                                      .hisOrderCode("addseeees")
                                      .serialNum(5)
                                      .location("dsdddd")
                                      .visitTime("2020-03-08 07:00:00")
                                      .visitDescription("334")
                                      .receiptNo("a112")
                                      .payCertificate("dddd")
                                      .settleTime("2020-03-08 07:00:00")
                                      .build();
                          }
                      }
        );
    }

    /**
     * 2.6 挂号取消
     *
     * @param apiAppId   接入账号
     * @param apiVersion 接口版本号
     * @param apiStamp   时间戳
     * @param apiSign    参数签名
     * @return 可挂号医生信息
     */
    @PostMapping("/cancel")
    @ApiOperation("2.6 挂号取消")
    public RespResult<Serializable> cancel(
            //接入账号(app_id)
            @RequestHeader("api_app_id") final String apiAppId,
            //接口版本号(采用正整数描述)
            @RequestHeader("api_version") final Integer apiVersion,
            //时间戳(13位到毫秒)
            @RequestHeader("api_stamp") final Long apiStamp,
            //参数签名
            @RequestHeader("api_sign") final String apiSign,
            @RequestBody(required = true) final Map<String, Serializable> req

    ) {
        return action(new Process<Serializable>() {
                          @Override
                          public String getAppId() {
                              return apiAppId;
                          }

                          @Override
                          public Integer getVersion() {
                              return apiVersion;
                          }

                          @Override
                          public Long getStamp() {
                              return apiStamp;
                          }

                          @Override
                          public String getSign() {
                              return apiSign;
                          }

                          @Override
                          public Map<String, Serializable> getParams() {
                              return req;
                          }

                          @Override
                          public Serializable handler() {
                              ///TODO: 业务处理
                              return null;
                          }
                      }
        );
    }

    /**
     * 2.7 挂号退费确认
     *
     * @param apiAppId   接入账号
     * @param apiVersion 接口版本号
     * @param apiStamp   时间戳
     * @param apiSign    参数签名
     * @return 可挂号医生信息
     */
    @PostMapping("/pre/refund")
    @ApiOperation("2.7 挂号取消")
    public RespResult<ResgiterPreRefund> preRefund(
            //接入账号(app_id)
            @RequestHeader("api_app_id") final String apiAppId,
            //接口版本号(采用正整数描述)
            @RequestHeader("api_version") final Integer apiVersion,
            //时间戳(13位到毫秒)
            @RequestHeader("api_stamp") final Long apiStamp,
            //参数签名
            @RequestHeader("api_sign") final String apiSign,
            @RequestBody(required = true) final Map<String, Serializable> req

    ) {
        return action(new Process<ResgiterPreRefund>() {
                          @Override
                          public String getAppId() {
                              return apiAppId;
                          }

                          @Override
                          public Integer getVersion() {
                              return apiVersion;
                          }

                          @Override
                          public Long getStamp() {
                              return apiStamp;
                          }

                          @Override
                          public String getSign() {
                              return apiSign;
                          }

                          @Override
                          public Map<String, Serializable> getParams() {
                              return req;
                          }

                          @Override
                          public ResgiterPreRefund handler() {
                              ///TODO: 业务处理
                              return ResgiterPreRefund.builder().result(true).build();
                          }
                      }
        );
    }

    /**
     * 2.8 挂号退费
     *
     * @param apiAppId   接入账号
     * @param apiVersion 接口版本号
     * @param apiStamp   时间戳
     * @param apiSign    参数签名
     * @return 可挂号医生信息
     */
    @PostMapping("/refund")
    @ApiOperation("2.8 挂号退费")
    public RespResult<RegisterRefund> registerRefund(
            //接入账号(app_id)
            @RequestHeader("api_app_id") final String apiAppId,
            //接口版本号(采用正整数描述)
            @RequestHeader("api_version") final Integer apiVersion,
            //时间戳(13位到毫秒)
            @RequestHeader("api_stamp") final Long apiStamp,
            //参数签名
            @RequestHeader("api_sign") final String apiSign,
            @RequestBody(required = true) final Map<String, Serializable> req

    ) {
        return action(new Process<RegisterRefund>() {
                          @Override
                          public String getAppId() {
                              return apiAppId;
                          }

                          @Override
                          public Integer getVersion() {
                              return apiVersion;
                          }

                          @Override
                          public Long getStamp() {
                              return apiStamp;
                          }

                          @Override
                          public String getSign() {
                              return apiSign;
                          }

                          @Override
                          public Map<String, Serializable> getParams() {
                              return req;
                          }

                          @Override
                          public RegisterRefund handler() {
                              ///TODO: 业务处理
                              return RegisterRefund.builder().hisRefundOrderCode("2334dee").build();
                          }
                      }
        );
    }


}
